package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.common.base.RespResult;
import com.hy.common.utils.MyObjectUtils;
import com.hy.entity.dto.LogShowDTO;
import com.hy.entity.pojo.SysOpLogReq;
import com.hy.mapper.LogMapper;
import com.hy.service.LogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class LogServiceImpl implements LogService {

    @Resource
    private LogMapper logMapper;

    @Override
    public RespResult showLog(LogShowDTO showDTO) {
        AtomicReference<List<SysOpLogReq>> sysOpLogReqs=new AtomicReference<>();
        AtomicReference<Integer> count= new AtomicReference<>(0);
        Optional.ofNullable(showDTO)
                .filter(MyObjectUtils::isNotBlank)
                .ifPresent(e->{
                    Page<SysOpLogReq> page=showDTO.getCurrentPage()!=null&&showDTO.getPageSize()!=null
                            ?new Page<>(showDTO.getCurrentPage(),showDTO.getPageSize()):new Page<>();
                    LambdaQueryWrapper<SysOpLogReq> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(MyObjectUtils.isNotBlank(showDTO.getMenuName()),SysOpLogReq::getMenuName,showDTO.getMenuName())
                            .eq(MyObjectUtils.isNotBlank(showDTO.getMenuBtn()),SysOpLogReq::getMenuBtn,showDTO.getMenuBtn())
                            .like(MyObjectUtils.isNotBlank(showDTO.getOpAccount()),SysOpLogReq::getOpAccount,showDTO.getOpAccount())
                            .like(MyObjectUtils.isNotBlank(showDTO.getOpContent()),SysOpLogReq::getOpContent,showDTO.getOpContent());
                    Page<SysOpLogReq> sysOpLogReqPage = logMapper.selectPage(page, lambdaQueryWrapper);
                    sysOpLogReqs.set(sysOpLogReqPage.getRecords());
                    count.set(Long.valueOf(sysOpLogReqPage.getTotal()).intValue());
                });
        return RespResult.success(sysOpLogReqs==null?null:sysOpLogReqs.get()).setCount(count.get());
    }
}
